package org.tokaf.normalizer;

import org.tokaf.TopKElement;

/**
 * <p>LevelNormalizer gets the number from object, using innerNormalizer , than
 * rounds it rating to nearest of given levels.</p> <p>Copyright (c) 2006</p>
 * @author Alan Eckhardt
 * @version 1.0
 */
public class LevelNormalizer implements Normalizer {
	Normalizer innerNormalizer;

	double[] levels;

	String userName;

	public LevelNormalizer(Normalizer innerNormalizer, double[] levels) {
		this.innerNormalizer = innerNormalizer;
		this.levels = levels;
		this.userName = innerNormalizer.getUserName();
	}

	public LevelNormalizer(Normalizer innerNormalizer, double[] levels,
			String userName) {
		this.innerNormalizer = innerNormalizer;
		this.levels = levels;
		this.userName = userName;
	}

	public double Normalize(TopKElement te, int index) {
		if (te == null)
			return 0;
		return Normalize(te.getRatingObject(index));
	}

	public double Normalize(Object o) {
		double resTemp = innerNormalizer.Normalize(o);
		double res = 0;
		for (int i = 0; i < levels.length; i++) {
			if (resTemp >= levels[i])
				res = levels[i];
		}
		return res;
	}

	public int compare(Object o1, Object o2) {
		double a = Normalize(o1);
		double b = Normalize(o2);
		if (a < b)
			return 1;
		if (a > b)
			return -1;
		return 0;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
		this.innerNormalizer.setUserName(userName);
	}

	public double[] getLevels() {
		return levels;
	}

	public void setLevels(double[] levels) {
		this.levels = levels;
	}

	public Normalizer getInnerNormalizer() {
		return innerNormalizer;
	}

	public void setInnerNormalizer(Normalizer innerNormalizer) {
		this.innerNormalizer = innerNormalizer;
	}

}
